import java.util.Stack;
import java.util.StringTokenizer;
public class Calculator {

    public static double evaluate(String expression) {
        String postfix = infixToPostfix(expression);
        return evaluatePostfix(postfix);
    }
    private static String infixToPostfix(String expression) {
        Stack<String> operatorStack = new Stack<>();
        Stack<String> outputStack = new Stack<>();
        StringTokenizer tokenizer = new StringTokenizer(expression, "()+-*/ ", true);
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken().trim();
            if (token.isEmpty()) continue;
            if (isOperator(token)) {
                while (!operatorStack.isEmpty() && precedence(operatorStack.peek()) >= precedence(token)) {
                    outputStack.push(operatorStack.pop());
                }
                operatorStack.push(token);
            } else if (token.equals("(")) {
                operatorStack.push(token);
            } else if (token.equals(")")) {
                while (!operatorStack.peek().equals("(")) {
                    outputStack.push(operatorStack.pop());
                }
                operatorStack.pop(); // Pop the '('
            } else {
                outputStack.push(token);
            }
        }
        while (!operatorStack.isEmpty()) {
            outputStack.push(operatorStack.pop());
        }
        StringBuilder postfix = new StringBuilder();
        while (!outputStack.isEmpty()) {
            postfix.insert(0, outputStack.pop() + " ");
        }
        return postfix.toString().trim();
    }

    private static double evaluatePostfix(String postfix) {
        Stack<Double> stack = new Stack<>();
        StringTokenizer tokenizer = new StringTokenizer(postfix, " ", false);
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            if (isOperator(token)) {
                double operand2 = stack.pop();
                double operand1 = stack.pop();
                double result = applyOperator(token, operand1, operand2);
                stack.push(result);
            } else {
                stack.push(Double.parseDouble(token));
            }
        }
        return stack.pop();
    }

    private static boolean isOperator(String token) {
        return token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/");
    }
    private static int precedence(String operator) {
        switch (operator) {
            case "+":
            case "-":
                return 1;
            case "*":
            case "/":
                return 2;
            default:
                return 0;
        }
    }
    private static double applyOperator(String operator, double operand1, double operand2) {
        double result = 0d;
        if("+".equals(operator)){
            result = operand1 + operand2;
        }else if("-".equals(operator)){
            result = operand1 - operand2;
        }else if("*".equals(operator)){
            result = operand1 * operand2;
        }else if("/".equals(operator)){
            result =operand1 / operand2;
        }
        return result;
    }

    public static void main(String[] args) {
        String expression = "(2+6)*3/4";
        System.out.println("Expression: " + expression);
        System.out.println("Result: " + evaluate(expression));
    }
}